package com.dfec.water.controller;

import com.dfec.common.enums.BusinessType;
import com.dfec.common.utils.AjaxResult;
import com.dfec.framework.auth.custom.controller.AbstractController;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.framework.mybatis.core.page.PageUtils;
import com.dfec.framework.operatelog.annotation.SysLog;
import com.dfec.water.param.AreaAddParam;
import com.dfec.water.param.AreaImgAddParam;
import com.dfec.water.service.YdbAreaImgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.dfec.framework.excel.service.ExcelService;
import java.util.List;
import java.util.Arrays;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.dfec.water.entity.YdbAreaBaseInfoEntity;
import com.dfec.water.service.YdbAreaBaseInfoService;

/**
 * 区域
 * @title: YdbAreaBaseInfoController
 * @author trg
 * @date Thu Jan 09 10:57:17 CST 2025
 */
@Tag(name = "区域选择")
@RestController
@RequestMapping("water/area/info")
public class YdbAreaBaseInfoController extends AbstractController {

    @Resource
    private  YdbAreaBaseInfoService ydbAreaBaseInfoService;

    @Resource
     private  ExcelService excelService;

    @Resource
     private  YdbAreaImgService imgService;

    /**
     * 分页查询
     */
    @PostMapping("/pageList")
    @Operation(summary = "分页查询")
    public AjaxResult<PageUtils<YdbAreaBaseInfoEntity>> pageList(@RequestBody PageParam<YdbAreaBaseInfoEntity> pageParam) {
        PageUtils page = ydbAreaBaseInfoService.queryPage(pageParam);
        return AjaxResult.ok().put(page);
    }


    /**
     * 不带分页查询
     */
    @PostMapping("/listAll")
    @Operation(summary = "全查")
    public AjaxResult<List<YdbAreaBaseInfoEntity>> listAll(@RequestBody YdbAreaBaseInfoEntity ydbAreaBaseInfo) {
        return AjaxResult.ok().put(ydbAreaBaseInfoService.listAll(ydbAreaBaseInfo));
    }


    /**
     * 保存
     */
    @SysLog(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @Operation(summary = "新增")
    public AjaxResult save(@RequestBody AreaAddParam areaAddParam) {
            ydbAreaBaseInfoService.saveArea(areaAddParam);
        return AjaxResult.ok();
    }


    /**
     * 保存
     */
    @SysLog(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/authDevice")
    @Operation(summary = "新增")
    public AjaxResult authDevice(@RequestBody AreaAddParam areaAddParam) {
        ydbAreaBaseInfoService.authDevice(areaAddParam);
        return AjaxResult.ok();
    }

    /**
     * 获取已授权列表
     */
    @GetMapping("/getAuthDevice")
    public AjaxResult getAuthDevice(@RequestParam Long areaId) {
        return AjaxResult.ok(ydbAreaBaseInfoService.getAuthDevice(areaId));
    }

    /**
     * 可视化-获取已授权列表
     */
    @GetMapping("/getAreaDevices")
    public AjaxResult getAreaDevices(@RequestParam Long areaId) {
        return AjaxResult.ok(ydbAreaBaseInfoService.getAreaDevices(areaId));
    }



    /**
     * 修改
     */
    @SysLog(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @Operation(summary = "修改")
    public AjaxResult update(@RequestBody YdbAreaBaseInfoEntity ydbAreaBaseInfo) {
            ydbAreaBaseInfoService.updateById(ydbAreaBaseInfo);
        return AjaxResult.ok();
    }

    /**
     * 删除
     */
    @SysLog(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @Operation(summary = "删除")
    public AjaxResult delete(@RequestBody Long[] ids) {
            ydbAreaBaseInfoService.removeByIds(Arrays.asList(ids));
        return AjaxResult.ok();
    }


    /**
     * 导入Excel
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam MultipartFile file) {
        excelService.importExcel(file, YdbAreaBaseInfoEntity.class,1,ydbAreaBaseInfoService::saveBatch);
        return AjaxResult.ok();
    }


    /**
     * 导入出Excel
     */
    @PostMapping("/exportExcel")
    public void export(HttpServletResponse response) throws IOException {
        excelService.exportExcel(ydbAreaBaseInfoService.list(), YdbAreaBaseInfoEntity.class, response);
    }



    /**
     * 保存图片
     */
    @SysLog(title = "保存图片", businessType = BusinessType.INSERT)
    @PostMapping("/saveImg")
    @Operation(summary = "新增")
    public AjaxResult saveImg(@RequestBody AreaImgAddParam param) {
        imgService.save(param);
        return AjaxResult.ok();
    }



    /**
     * 可视化-获取已授权列表
     */
    @GetMapping("/getImgList")
    public AjaxResult getImgList(@RequestParam Long areaId) {
        return AjaxResult.ok(imgService.getImgList(areaId));
    }
}
